package com.qianli.cashmama.trade.openApi.job;

import com.fqgj.common.utils.TimeUtils;
import com.fqgj.xjd.user.client.enums.FrozenCodeEnum;
import com.qianli.cashmama.trade.common.enums.TradeStatusEnum;
import com.qianli.cashmama.trade.common.utils.TradeConfigUtil;
import com.qianli.cashmama.trade.dao.TTradeDao;
import com.qianli.cashmama.trade.domain.model.TradeBase;
import com.qianli.cashmama.trade.domain.services.TradeBaseService;
import com.qianli.cashmama.trade.entity.TTradeEntity;
import com.qianli.cashmama.trade.integration.user.UserIntegrationService;
import com.qianli.cashmama.trade.repo.TradeBaseRepository;
import com.qianli.cashmama.trade.unique.TradeUniqueService;
import com.robert.vesta.util.IpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2021/12/3
 * Time: 10:56 上午
 */

@Service
public class RejectJob {

    private static final Logger logger = LoggerFactory.getLogger(RejectJob.class);
    @Autowired
    private TradeConfigUtil configUtil;

    @Autowired
    private TradeBaseRepository tradeBaseRepository;

    @Autowired
    private TradeBaseService tradeBaseService;

    @Autowired
    private TradeUniqueService tradeUniqueService;

    @Autowired
    private UserIntegrationService userIntegrationService;

    @Autowired
    private TTradeDao tTradeDao;

    @Scheduled(fixedDelay = 1000L * 600)
    public void run() {

        String hostIp = IpUtils.getHostIp();
        logger.info("==RejectJob==,ip:{}", hostIp);

        if (!hostIp.equals(configUtil.pushJobRunIp())) {
            logger.info("==RejectJob==, 当前机器ip不是job执行机器,当前iP:{},目标IP:{}", hostIp, configUtil.pushJobRunIp());
            return;
        }

        List<TTradeEntity> tTradeEntities = tTradeDao.selectByOrderStatus(TradeStatusEnum.AUDIT_WATTING.getStatus());
        logger.info("== 拒绝任务 开始 ==,size:{}", tTradeEntities.size());

        tTradeEntities.forEach(tTradeEntity -> {
            boolean isRejectCategory = configUtil.getAutoRejectCategories().contains(tTradeEntity.getProductCategory());
            if (!isRejectCategory) {
                logger.info("== 拒绝任务 ==，isRejectCategory:{},category:{}", isRejectCategory, tTradeEntity.getProductCategory());
                return;
            }

            logger.info("== 拒绝任务对象==,category:{}", tTradeEntity.getProductCategory());
            if (!configUtil.getRejectJobSwitch()) {
                logger.info("== 拒绝任务没有开启 ==");
                return;
            }


            try {
                TradeBase entity = tradeBaseRepository.load(tTradeEntity.getTradeNo());
                if (entity.getTradeOrderStatus() == TradeStatusEnum.AUDIT_WATTING) {
                    tradeBaseService.closeAuditRejectTrade(entity);
                    tradeUniqueService.releaseUniqueLock(entity);
                    tradeBaseRepository.restore(entity);
                    userIntegrationService.frozenUser(entity.getUserCode(), entity.getProductCategory(), FrozenCodeEnum.BORROW_REJECT);
                }
            } catch (Exception e) {
                logger.error("== 拒绝任务 异常 ==,size:{} tradeNo:{}", tTradeEntity.getTradeNo(), e);
            }
        });


    }

}
